// Created By:      Roy Kravitz
// Modified By:     Jabeer Ahmed
// Last Modified:   (JA) 23-Jan-2014
//
// Revision History:
// -----------------
// Nov-2008     RK      Created this module for the S3E Starter Board
// Apr-2012     DG      Modified for Nexys 3 board
// Dec-2014     RJ      Cleaned up formatting.  No functional changes
// Jan-2014     JA      Converting Getting Started Guide to RojoBot project - 1
// 
// Description:
// ------------
// Top level module for the ECE 540 Project - 1 (RojoBot), built 
// on the Nexys3 FPGA Board (Xilinx XC6LX16-CS324). The module contains:
//      * BotSim (Rojo Bot)
//      * Picoblaze CPU - to run RojoBot control/path navigation logic
//      * Interface unit between BotSim <-> Picoblaze controls and sensors
//      * Nexys3 Pushbutton controller
//      * Nexys3 Slide switches controller
//      * Nexys3 Pushbutton controller
//
// Use the pushbuttons to control the Rojobot wheels:
//  btnl    Left wheel forward
//  btnu    Left wheel reverse
//  btnr    Right wheel forward
//  btnd    Right wheel reverse
//  btns    System reset
//
//  Switches are not used. Compass heading and a turn indicator 
//  are shown on the seven segment dispay.  LED's display the chase segments.  
///////////////////////////////////////////////////////////////////////////

module Nexys3fpga (
    // Inputs
    input               clk100,         // 100MHz clock from on-board oscillator
    input               btnl, btnr,     // pushbutton inputs - left and right
    input               btnu, btnd,     // pushbutton inputs - top and bottom
    input               btns,           // pushbutton inputs - center button
    input   [7:0]       sw,             // switch inputs
    // Outputs    
    output  [7:0]       led,            // LED outputs  
    output  [7:0]       seg,            // Seven segment display cathode pins
    output  [3:0]       an,             // Seven segment display anode pins     
    output  [3:0]       JA,             // JA Header

    output              Vsync,
    output              Hsync,
    output  [2:0]       vgaRed,
    output  [2:0]       vgaGreen,
    output  [2:0]       vgaBlue
); 
    
/*******************************************************************************
                            Internal Variables
 ******************************************************************************/
    wire                sysclk;         // 100MHz clock from on-board oscillator    
    wire                sysreset;       // system reset signal    
    wire    [31:0]      digits_out;     // ASCII digits (Only for Simulation)
    wire    [7:0]       db_sw;          // debounced switches
    wire    [4:0]       db_btns;        // debounced buttons

    /********************************* BotSIM *********************************/ 

    wire    [7:0]       bot_locX;
    wire    [7:0]       bot_locY;
    wire    [7:0]       bot_botinfo;
    wire    [7:0]       bot_sensors;
    wire    [7:0]       bot_Imdist;
    wire    [7:0]       bot_rmdist;
    wire                bot_upd_sysregs;
	wire 	[3:0] 		bot_pbtn_db;
    wire    [1:0]       bot_world_pxl_out;
    
    /**************************** Video Controller ****************************/
    wire    [9:0]       vc_vid_row_out;
    wire    [9:0]       vc_vid_col_out;

    /**************************** Debounce Module *****************************/

    // global assigns
    assign  sysclk      = clk100;
    assign  sysreset    = db_btns[0];
	 assign 	bot_pbtn_db = db_btns[4:1];
    assign  JA          = {sysclk, sysreset, 2'b0};

/*******************************************************************************
                            Submodule Instantiation
 ******************************************************************************/

    /********************************* botSIM *********************************/
    bot botSIM(
        .MotCtl_in      (8'b0),  
        .LocX_reg       (bot_locX),          
        .LocY_reg       (bot_locY),          
        .Sensors_reg    (),    
        .BotInfo_reg    (bot_botinfo),    
        .LMDist_reg     (),      
        .RMDist_reg     (),      
        .upd_sysregs    (),
        .clk            (sysclk),        
        .reset          (),

        /****** video controller ******/  
        .vid_row        (vc_vid_row_out),
        .vid_col        (vc_vid_col_out),
        .vid_pixel_out  (bot_world_pxl_out)    
    );
    
    /**************************** Debounce module *****************************/
    debounce    DB  (
        .clk            (sysclk),   
        .pbtn_in        ({btnl,btnu,btnr,btnd,btns}),
        .switch_in      (sw),
        .pbtn_db        (db_btns),
        .swtch_db       (db_sw)
    );  

    /************************* Video Controller **************************/	
	VideoController_IF vidCtl (
        .clk100         (sysclk),        
        .reset          (), 
        .world_pixel_in (bot_world_pxl_out), 
        .BotInfo_in     (bot_botinfo),     
        .Loc_x_in       (bot_locX),       
        .Loc_y_in       (bot_locY),

        .pxl_row        (vc_vid_row_out),
        .pxl_col        (vc_vid_col_out),       
        .vert_sync      (Vsync),
        .horz_sync      (Hsync),
        .red            (vgaRed),
        .green          (vgaGreen),
        .blue           (vgaBlue[2:1])         	
	);

endmodule
